<?php

	namespace Admin\Model;
	use Think\Model;

	class ClassifyModel extends Model{

		protected $_validate = array(
			array('name','require','填写的分类名称有误' , 1),
			array('id','require','填写的分类名称有误' , 1),
		);

		
		//显示分类列表显示
		public function gain($data)
		{
			$sign = ['禁用' , '启用'];

			// 查询分类表
			foreach ($data as $key => &$value) {
				// 改变状态
				$value['status'] = $sign[$value['status']];
				$value['addtime'] = date('Y/m/d' , $value['addtime']);
				// 1.获取逗号的数量
				$num = substr_count($value['path'] , ',');
				//  2.重复一个字符串
        		$str = str_repeat('|---',$num - 1 );
        		$value['name'] = $str . $value['name'];
			}
			return $data;
		}
		//展示是否添加顶级还是子级
		public function pro_Show()
		{
			$get = I('get.');
			$info = [];
			if (empty(I('get.id'))) {
				$info['id'] = $get['id'] + 0;
				$info['choose'] = '添加顶级商品分类';
				return $info;
			}else{
				$info['id'] = $get['id'] + 0;


				// 面包线
				$cid = $get['id'] + 0;
				$classify = M('classify');
				$map['id'] = ['eq' , $cid];
				$path = $classify->field('path , name')->where($map)->select()[0];

				$bread = [];
				$num = substr_count($path['path'] , ',');
				// 如果是顶级为1 二级为2 三级为3
				if ($num == 1) {
					$bread[0] = $path['name'];
				}else if($num == 2){
					$pa = explode("," , $path['path'] );
					$map['id'] = ['eq' , $pa[1]];
					$bread[0] = $classify->field('name')->where($map)->select()[0]['name'];
					$bread[1] = $path['name'];


				}
				$bread = implode('->',$bread); 
				// 面包线结束

				$info['choose'] = '您正在为 ( ' . $bread  . ' ) 添加子分类';
				return $info;
			}
		}


		// 添加分类
		public function pro_Add()
		{

			$post = I('post.');

			// 判断添加顶级还是分类
			if (empty($post['id'])) {

				// 进行自动验证
				$sign = $this->create($post);

				if ($sign) {
					// 添加的顶级分类数据的属性
					$list['name'] = strip_tags($post['name']);
					$list['name'] = trim(($post['name']));
					$list['pid']  = 0;
					$list['path'] = '0,';
					$list['status'] = 1;
					$list['addtime'] = time();
					// 添加到数据库
					$res = $this->add($list);
					return $res;

				}else{
					// 如果被恶意修改前台的HTML文件时触发
					$this->success('别玩哦,非法数据', U('increase'));
				}

			}else{

				// 进行自动验证
				$sign = $this->create($post);
				// 添加子分类
				if ($sign) {

					//查询父级ID的相对路径
					$path = $this->field()->where(['id' => ['eq' , $post['id']]])->find()['path'];
					$path = $path . $post['id'] . ',';
					$list['name'] = strip_tags($post['name']);
					$list['name'] = trim(($post['name']));
					$list['pid']  = $post['id'];
					$list['path'] = $path;
					$list['status'] = 1;
					$list['addtime'] = time();
					$res = $this->add($list);
					return $res;

				}else{
					$errorinfo = $this->getError();
					redirect(U('Classify/increase'),  5, '添加顶级失败' . $errorinfo . '页面跳转中...');
				}
			}
		}

		public function pro_status(){
			// 获取修改状态的ID
			$id = I('get.id') + 0;

			$res = $this->jinyong($id);

			// 获取当前状态
			$status = I('get.status');

			// 需要取反状态
			if ($status == '启用') {
				echo $res;
				$data['status'] = 0;
			}else{
				// 判断父级是否为禁用状态
				$haha = $this->qiyong($id);
				if ($haha == '不能启用') {
					echo '不能启用';
					exit;
				}else{
					echo $res;
				}

				$data['status'] = 1;
			}
			// 需要修改状态的ID
			// $map['id'] = ['eq' , $id];
			// 修改
			$res = $this->where("id in ($res)")->save($data);
			// return $res;
		}

		// 启用
		public function qiyong($id){
			$status = M('classify')->where("id = $id")->field('pid , status')->select();

			// 判断上面的分类级别是否被禁用,如果上级禁用了,下级就不能启用
			if ($status[0]['pid'] != 0) {

				//如果上级不是顶级
				$id = $status[0]['pid'];
				$status1 = M('classify')->where("id = $id")->field('pid , status')->select();

				if ($status1[0]['pid'] == 0) {
					// 第二级
					if ($status1[0]['status'] == 0) {
						
						return '不能启用';
					}
				}else{
					if ($status1[0]['status'] == 0) {
						
						return '不能启用';
					}else{
						// 第三级
						$id = $status1[0]['pid'];
						$status2 = M('classify')->where("id = $id")->field('pid , status')->select();

						if ($status2[0]['status'] == 0) {
							return '不能启用';
						}
					}
				}
			}else{
				// 如果等于0是顶级
				// return '不能启用';
			}
		}

		// 禁用
		public function jinyong($id){
			// 判断是否是为顶级二级
			$pid = D('classify')->where("pid={$id}")->select();
			if ($pid) {
				$res = M('classify')->where("path like '%,{$id},%'")->field('id')->select();
	            foreach($res as $v){
	                $str .= $v['id'].',';
	            }
	         	return $res = rtrim($str,',') . ',' . $id;
      
			}else{
				return $res = $id;
			}

		}

		// 修改名称
		public function pro_modify()
		{
			// 接受数据
			$id = I('post.id');
			$name = I('post.name');
			$data['name'] = $name;
			$data['name'] = htmlentities($data['name']);
			$data['name'] = strip_tags($data['name']);
			$data['name'] = trim($data['name']);
			$map['id'] = ['eq' , $id];
			$res = $this->where($map)->save($data);
			if ($res == 0) {
				return false;
			}else{
				return true;
			}
		}





	}
